package com.monster.skeleton
{
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	CONFIG::release
		{
			import flash.utils.ByteArray;
			import cmodule.CModule;
			import com.monster.abacus.movieclip.loader.cc_decompressMovieClip;
		};
	
	public class AppLoader
	{
		private var _compressMainLoader:URLLoader;
		private var _mainLoader:Loader;

		private var _container:DisplayObjectContainer;
		private var _mainSwfName:String;
		private var _cbComplete:Function;
		private var _cbFail:Function;
		private var _cbProgress:Function;

		public function AppLoader(container:DisplayObjectContainer, mainSwfName:String, cbComplete:Function, cbFail:Function, cbProgress:Function)
		{
			_container = container;
			_mainSwfName = mainSwfName;
			_cbComplete = cbComplete;
			_cbFail = cbFail;
			_cbProgress = cbProgress;
		}
		
		public function start():void
		{
			createMainLoader();
			
			var url:String;
			if(CONFIG::release)
			{
				url = _mainSwfName + "." + "s" + "w" + "b";
				_compressMainLoader.load(new URLRequest(url));
			}
			else
			{
				url = _mainSwfName + "." + "s" + "w" + "f";
				_mainLoader.load(new URLRequest(url));
			}
			
			CONFIG::debug
				{
					TRACE("load game main, url = " + url);
				};
		}
		
		private function createMainLoader():void
		{
			if(CONFIG::release)
			{
				_compressMainLoader = new URLLoader();
				_compressMainLoader.dataFormat = URLLoaderDataFormat.BINARY;
				_compressMainLoader.addEventListener(Event.COMPLETE, onComplete);
				_compressMainLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
				_compressMainLoader.addEventListener(ProgressEvent.PROGRESS, onProgress);
			}
			else
			{
				_mainLoader = new Loader();
				_mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
				_mainLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
				_mainLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			}
		}
		
		private function freeMainLoader():void
		{
			if(CONFIG::release)
			{
				_compressMainLoader.removeEventListener(Event.COMPLETE, onComplete);
				_compressMainLoader.removeEventListener(IOErrorEvent.IO_ERROR, onError);
				_compressMainLoader.removeEventListener(ProgressEvent.PROGRESS, onProgress);
				_compressMainLoader = null;
			}
			else
			{
				_mainLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
				_mainLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
				_mainLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
				_mainLoader = null;
			}
		}
		
		private function onComplete(e:Event):void
		{
			if(CONFIG::release)
			{
				CONFIG::release
					{
						var stream:ByteArray = _compressMainLoader.data;
						var pBuffer:int = CModule.malloc(stream.length);
						CModule.writeBytes(pBuffer, stream.length, stream);
						cc_decompressMovieClip(pBuffer, stream.length, _container, onLoad);
						
						function onLoad():void
						{
							CModule.free(pBuffer);
							pBuffer = 0;
						}
					};
			}
			else
			{
				_container.addChild(_mainLoader);
			}
			
			freeMainLoader();
			
			if(_cbComplete != null)
			{
				_cbComplete(e);
			}
		}
		
		private function onError(e:IOErrorEvent):void
		{
			freeMainLoader();
			
			if(_cbFail != null)
			{
				_cbFail(e);
			}
		}
		
		private function onProgress(e:ProgressEvent):void
		{
			if(_cbProgress != null)
			{
				_cbProgress(e);
			}
		}
		

	}
}